<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人资料</title>
    <link rel="stylesheet" href="../css/indivisual-info.css">
    <script src="../js/jquery.mini.js"></script>
</head>

<body>
    <div class="center">
        <h3>修改个人资料</h3>
        <div class="head">
            <img id="head-pic" src="" alt="">
            <div class="chang-pic-btn">更改头像</div>
            <input type="file" id="file" style="display: none;">
        </div>
        <div class="input-each"><span class="tip-info">昵称</span><input type="text" id="nickname"></div>
        <div class="input-each"><span class="tip-info">学校</span><input type="text" id="school"></div>
        <div class="input-each"><span class="tip-info">专业</span><input type="text" id="marjor"></div>
        <div class="input-each"><span class="tip-info">性别</span><input type="text" id="sex"></div>
        <div class="input-each"><span class="tip-info">年级</span><input type="text" id="grade"></div>
        <div class="input-each"><span class="tip-info">年龄</span><input type="text" id="age"></div>
        <div class="input-each"><span class="tip-info">住址</span><input type="text" id="address"></div>
        <div class="chang-btn">修改</div>
    </div>

</body>
<script>
    $(function () {
        $.ajax({
            url: "/users/get_by_uid",
            type: "get",
            dataType: "json",
            success: function (result) {
                if (result.state === 200) {
                    var data = result.data;
                    $('#nickname').val(data.nickname);
                    $('#age').val(data.age);
                    $('#school').val(data.school);
                    $('#sex').val(data.sex);
                    $('#marjor').val(data.marjor);
                    $('#grade').val(data.grade);
                    $('#address').val(data.address);
                    $('#head-pic').attr("src", data.img_path);
                }
                else {
                    alert("用户资料加载失败！");
                }
            }
        });
        var img_path;
        $('.chang-pic-btn').click(function () {
            $("#file").click();
        });
        $("#file").change(function () {
            var file = $("#file")[0].files[0];
            var formData = new FormData();
            formData.append("file", file);
            $.ajax({
                url: "/users/uploadimg",
                type: "POST",
                data: formData,
                dataType: "json",
                processData: false,	// 使数据不做处理
                contentType: false,		// 不要设置Content-Type请求头
                success: function (result) {
                    alert("头像上传成功");
                    if (result.state === 200) {
                        // alert("goods_path" + result.data);
                        img_path = "/pic/head-pic/" + result.data;
                        $('#head-pic').attr("src", img_path);
                    } else {
                        alert("头像上传失败！");
                    }
                },
                error: function (result) {
                    alert(reult.state);
                }
            });
        });
        $(".chang-btn").click(function () {
            var nickname = $('#nickname').val();
            var age = $('#age').val();
            var school = $('#school').val();
            var marjor = $('#marjor').val();
            var grade = $('#grade').val();
            var sex = $('#sex').val();
            var address = $('#address').val();
            var json = JSON.stringify({ "sex": sex, "nickname": nickname, "age": age, "school": school, "marjor": marjor, "grade": grade, "img_path": img_path });
            alert(json);
            console.log(json);
            $.ajax({
                url: "/users/change_info",
                type: "post",
                data: json,
                dataType: "json",
                contentType: "application/json",
                success: function (result) {
                    if (result.state === 200) {
                        alert("修改个人信息成功！");
                        sessionStorage.setItem("imgpath", img_path);
                        // window.location.href = "goods.html?goods_id=" + result.data;
                    } else {
                        alert("修改个人信息失败！");
                    }
                }
            });
        });
    })
</script>

</html>